package com.qdlc.p2p.dal.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.dao.UserVipUprateCollectionDao;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserVipUprateCollection;
@Repository("userVipUprateCollectionDao")
public class UserVipUprateCollectionDaoImpl extends BaseDaoImpl<UserVipUprateCollection> implements
		UserVipUprateCollectionDao {

	@Override
	public UserVipUprateCollection getUserVipUprateCollByCollId(
			long collectionId) {
		return super.findObjByProperty("collectionId", collectionId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getVipUpRateCollectionInterest(User user, String date) {
		String sql = "select DATE_FORMAT(repayment_time,'%m-%d'),sum(repay_interest) from " + Global.DB_PREFIX + "user_vip_uprate_collection where status = 0  and DATE_FORMAT(repayment_time,'%Y-%m') = :timeDate and user_id = :userId group by repayment_time ";
		Query q = em.createNativeQuery(sql).setParameter("timeDate", date).setParameter("userId", user.getUserId());
	    return q.getResultList();
	}
	
	@Override
	public double vipUpRateCollectionSum(long borrowId, int period) {
		String sql = "select sum(repay_interest) from " + Global.DB_PREFIX + "user_vip_uprate_collection where borrow_id = :borrowId and period = :period and status = 0";
		Query query = em.createNativeQuery(sql).setParameter("borrowId", borrowId).setParameter("period", period);
		Object obj = query.getSingleResult();
		if (obj != null) {
			return Double.parseDouble(obj.toString());
		}
		return 0;
	}
}
